<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>webpack学习总结(一) | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="webpack,">
  

  <meta name="description" content="声明 本教程整理于互联网  先看一个例子 // webpack.config.jsmodule.exports = &amp;#123;    // 入口    entry: &amp;#123;        &apos;index&apos;: &apos;./index.js&apos;    &amp;#125;,    // 输出    output: &amp;#123;        path: &apos;./&apos;,        /*            [">
<meta name="keywords" content="webpack">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack学习总结(一)">
<meta property="og:url" content="http://blog.poetries.top/2017/01/13/webpack/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="声明 本教程整理于互联网  先看一个例子 // webpack.config.jsmodule.exports = &amp;#123;    // 入口    entry: &amp;#123;        &apos;index&apos;: &apos;./index.js&apos;    &amp;#125;,    // 输出    output: &amp;#123;        path: &apos;./&apos;,        /*            [">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.942Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="webpack学习总结(一)">
<meta name="twitter:description" content="声明 本教程整理于互联网  先看一个例子 // webpack.config.jsmodule.exports = &amp;#123;    // 入口    entry: &amp;#123;        &apos;index&apos;: &apos;./index.js&apos;    &amp;#125;,    // 输出    output: &amp;#123;        path: &apos;./&apos;,        /*            [">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#一、参数详解"><span class="toc-text">一、参数详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-entry"><span class="toc-text">1.1 entry</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-output"><span class="toc-text">1.2 output</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-filename"><span class="toc-text">1.3 filename</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4-源码和构建结果"><span class="toc-text">1.4 源码和构建结果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、JS包含样式"><span class="toc-text">二、JS包含样式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-module-loaders-test"><span class="toc-text">2.1 module.loaders[].test</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-module-loaders-loader"><span class="toc-text">2.2 module.loaders[].loader</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-require-时配置-loader"><span class="toc-text">2.3 require 时配置 loader</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-4-被嵌入-lt-head-gt"><span class="toc-text">2.4 被嵌入 &lt;head&gt;</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、JS包含图片"><span class="toc-text">三、JS包含图片</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四、使用全局变量"><span class="toc-text">四、使用全局变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#五、暴露全局变量"><span class="toc-text">五、暴露全局变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#六、提取单独样式文件"><span class="toc-text">六、提取单独样式文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#七、自动打包公用资源"><span class="toc-text">七、自动打包公用资源</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#八、文件指纹-hash"><span class="toc-text">八、文件指纹 hash</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#九、异步加载"><span class="toc-text">九、异步加载</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-webpack" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">webpack学习总结(一)</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2017.01.13</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Build/">Build</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 953字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 4分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <blockquote>
<p>声明 本教程整理于互联网</p>
</blockquote>
<p>先看一个例子</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// webpack.config.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">// 入口</span></span><br><span class="line">    entry: &#123;</span><br><span class="line">        <span class="string">'index'</span>: <span class="string">'./index.js'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 输出</span></span><br><span class="line">    output: &#123;</span><br><span class="line">        path: <span class="string">'./'</span>,</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            [name] 是 entry 中的 key</span></span><br><span class="line"><span class="comment">            entry: &#123;</span></span><br><span class="line"><span class="comment">                key: value</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        filename: <span class="string">"[name].b.js"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>module.exports 是 CommonJS 规范中定义一个文件对外接口的语法，<a href="webpack.config.js">webpack.config.js</a> 文件对外的接口是一个 object ，其中定义了一些配置参数。</p>
<a id="more"></a>
<h3 id="一、参数详解"><a href="#一、参数详解" class="headerlink" title="一、参数详解"></a>一、参数详解</h3><hr>
<h4 id="1-1-entry"><a href="#1-1-entry" class="headerlink" title="1.1 entry"></a>1.1 entry</h4><hr>
<p>最初 webpack 是为了构建 SPA (Single Page Application) ，<code>entry</code> 是『入口』配置。在 <code>entry</code> 中的文件才会被编译。</p>
<h4 id="1-2-output"><a href="#1-2-output" class="headerlink" title="1.2 output"></a>1.2 output</h4><hr>
<p><code>output</code> 控制构建后的文件的存放位置和命名。 <code>path</code> 定义所有构建后文件的所在目录，本例中构建到当前文件夹。</p>
<h4 id="1-3-filename"><a href="#1-3-filename" class="headerlink" title="1.3 filename"></a>1.3 filename</h4><hr>
<p><code>filename</code> 控制构建后文件的文件名</p>
<h4 id="1-4-源码和构建结果"><a href="#1-4-源码和构建结果" class="headerlink" title="1.4 源码和构建结果"></a>1.4 源码和构建结果</h4><hr>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// index.js</span></span><br><span class="line"><span class="keyword">var</span> content = <span class="built_in">require</span>(<span class="string">"./content.js"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.body.innerHTML = <span class="built_in">document</span>.body.innerHTML + content</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// content.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="string">"some string"</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">target</span>=<span class="string">"_blank"</span> <span class="attr">href</span>=<span class="string">"https://github.com/nimojs/webpack-book/blob/gh-pages/1-modules/README.md"</span>&gt;</span>本例说明<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"index.b.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>建议尽量理解构建后的代码，这样有助于理解 <code>webpack</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/******/</span> (<span class="function"><span class="keyword">function</span>(<span class="params">modules</span>) </span>&#123; <span class="comment">// webpackBootstrap</span></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// The module cache</span></span><br><span class="line"><span class="comment">/******/</span> 	<span class="keyword">var</span> installedModules = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// The require function</span></span><br><span class="line"><span class="comment">/******/</span> 	<span class="function"><span class="keyword">function</span> <span class="title">__webpack_require__</span>(<span class="params">moduleId</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 		<span class="comment">// Check if module is in cache</span></span><br><span class="line"><span class="comment">/******/</span> 		<span class="keyword">if</span>(installedModules[moduleId])</span><br><span class="line"><span class="comment">/******/</span> 			<span class="keyword">return</span> installedModules[moduleId].exports;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 		<span class="comment">// Create a new module (and put it into the cache)</span></span><br><span class="line"><span class="comment">/******/</span> 		<span class="keyword">var</span> <span class="built_in">module</span> = installedModules[moduleId] = &#123;</span><br><span class="line"><span class="comment">/******/</span> 			exports: &#123;&#125;,</span><br><span class="line"><span class="comment">/******/</span> 			id: moduleId,</span><br><span class="line"><span class="comment">/******/</span> 			loaded: <span class="literal">false</span></span><br><span class="line"><span class="comment">/******/</span> 		&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 		<span class="comment">// Execute the module function</span></span><br><span class="line"><span class="comment">/******/</span> 		modules[moduleId].call(<span class="built_in">module</span>.exports, <span class="built_in">module</span>, <span class="built_in">module</span>.exports, __webpack_require__);</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 		<span class="comment">// Flag the module as loaded</span></span><br><span class="line"><span class="comment">/******/</span> 		<span class="built_in">module</span>.loaded = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 		<span class="comment">// Return the exports of the module</span></span><br><span class="line"><span class="comment">/******/</span> 		<span class="keyword">return</span> <span class="built_in">module</span>.exports;</span><br><span class="line"><span class="comment">/******/</span> 	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// expose the modules object (__webpack_modules__)</span></span><br><span class="line"><span class="comment">/******/</span> 	__webpack_require__.m = modules;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// expose the module cache</span></span><br><span class="line"><span class="comment">/******/</span> 	__webpack_require__.c = installedModules;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// __webpack_public_path__</span></span><br><span class="line"><span class="comment">/******/</span> 	__webpack_require__.p = <span class="string">""</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/******/</span> 	<span class="comment">// Load entry module and return exports</span></span><br><span class="line"><span class="comment">/******/</span> 	<span class="keyword">return</span> __webpack_require__(<span class="number">0</span>);</span><br><span class="line"><span class="comment">/******/</span> &#125;)</span><br><span class="line"><span class="comment">/************************************************************************/</span></span><br><span class="line"><span class="comment">/******/</span> ([</span><br><span class="line"><span class="comment">/* 0 */</span></span><br><span class="line"><span class="comment">/***/</span> <span class="function"><span class="keyword">function</span>(<span class="params">module, exports, __webpack_require__</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> content = __webpack_require__(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">	<span class="built_in">document</span>.body.innerHTML = <span class="built_in">document</span>.body.innerHTML + content</span><br><span class="line"></span><br><span class="line"><span class="comment">/***/</span> &#125;,</span><br><span class="line"><span class="comment">/* 1 */</span></span><br><span class="line"><span class="comment">/***/</span> <span class="function"><span class="keyword">function</span>(<span class="params">module, exports</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">module</span>.exports = <span class="string">"some string"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/***/</span> &#125;</span><br><span class="line"><span class="comment">/******/</span> ]);</span><br></pre></td></tr></table></figure>
<p>前面带 <code>/******/</code>的代码都是 <code>webpack</code>的模块化代码，它内置了一个模块加载器</p>
<p>模块 0 是 <code>index.js</code> 的代码，模块 1 是 <code>require(&quot;./content.js&quot;)</code> 的代码。如果你再  <code>require</code> 一个模块那么就会有模块 3。</p>
<h3 id="二、JS包含样式"><a href="#二、JS包含样式" class="headerlink" title="二、JS包含样式"></a>二、JS包含样式</h3><hr>
<p>安装时间可能会比较久</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 安装所需 style-loader 和 css-loader</span></span><br><span class="line">npm install css-loader style-loader -D</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> less 和 less-loader 可以选择不安装 （如果安装了请去掉 index.js 中引入 less 的注释）</span></span><br><span class="line">npm install less-loader -D</span><br><span class="line">npm install less -g</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack --watch</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    entry: &#123;</span><br><span class="line">        <span class="string">'index'</span>: <span class="string">'./index.js'</span></span><br><span class="line">    &#125;,    </span><br><span class="line">    output: &#123;</span><br><span class="line">        path: <span class="string">'./'</span>,</span><br><span class="line">        filename: <span class="string">"[name].b.js"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="built_in">module</span>: &#123;</span><br><span class="line">        loaders: [</span><br><span class="line">            &#123;</span><br><span class="line">                test: <span class="regexp">/\.css$/</span>,</span><br><span class="line">                loader: <span class="string">"style!css"</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">                test: <span class="regexp">/\.less$/</span>,</span><br><span class="line">                loader: <span class="string">"style!css!less"</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><code>module.loaders</code> 定义 <code>require</code> 的模块代码会被如何编译。 <a href="http://webpack.github.io/docs/using-loaders.html" target="_blank" rel="noopener">官方文档 using-loaders</a></p>
<h4 id="2-1-module-loaders-test"><a href="#2-1-module-loaders-test" class="headerlink" title="2.1 module.loaders[].test"></a>2.1 module.loaders[].test</h4><hr>
<p><code>test</code> 参数是一个正则表达式，用于匹配模块。<code>&#39;./index.css&#39;.test(/\.css&amp;/)</code></p>
<h4 id="2-2-module-loaders-loader"><a href="#2-2-module-loaders-loader" class="headerlink" title="2.2 module.loaders[].loader"></a>2.2 module.loaders[].loader</h4><hr>
<p><code>loader</code> 参数定义被 <code>test</code> 匹配到的模块会执行哪些构建操作</p>
<p>本例中 .css 后缀的文件会被 <code>style-loader</code> 和 <code>css-loader</code> 构建</p>
<h4 id="2-3-require-时配置-loader"><a href="#2-3-require-时配置-loader" class="headerlink" title="2.3 require 时配置 loader"></a>2.3 require 时配置 loader</h4><hr>
<p>如果你不想在 webpack.config.js 配置，你还可以在 require 时单独定义一个文件会使用哪些 loader</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">require</span>(<span class="string">'!style!css!./index.css'</span>)</span><br></pre></td></tr></table></figure>
<h4 id="2-4-被嵌入-lt-head-gt"><a href="#2-4-被嵌入-lt-head-gt" class="headerlink" title="2.4 被嵌入 &lt;head&gt;"></a>2.4 被嵌入 <code>&lt;head&gt;</code></h4><hr>
<p>查看 <a href="http://nimojs.github.io/webpack-book/2-style/" target="_blank" rel="noopener">在线预览构建结果页面</a> 源码可以看到样式通过 JS 嵌入到 <code>&lt;head&gt;</code> 中使用的<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span>body &#123;</span><br><span class="line"><span class="css">    <span class="selector-tag">background-color</span>:<span class="selector-id">#ABCDEF</span>;</span></span><br><span class="line">&#125;<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="三、JS包含图片"><a href="#三、JS包含图片" class="headerlink" title="三、JS包含图片"></a>三、JS包含图片</h3><hr>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">npm install url-loader file-loader -D</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack --watch</span><br></pre></td></tr></table></figure>
<blockquote>
<p>代码解释待补充</p>
</blockquote>
<h3 id="四、使用全局变量"><a href="#四、使用全局变量" class="headerlink" title="四、使用全局变量"></a>四、使用全局变量</h3><hr>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack -w</span><br></pre></td></tr></table></figure>
<h3 id="五、暴露全局变量"><a href="#五、暴露全局变量" class="headerlink" title="五、暴露全局变量"></a>五、暴露全局变量</h3><hr>
<blockquote>
<p>有些模块依赖全局变量 <code>windows.jQuery</code> 才能使用，可以通过 <a href="https://github.com/webpack/expose-loader" target="_blank" rel="noopener">expose-loader</a> 暴露全局变量</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">npm install jquery --save</span><br><span class="line">npm install expose-loader -D</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack -w</span><br></pre></td></tr></table></figure>
<h3 id="六、提取单独样式文件"><a href="#六、提取单独样式文件" class="headerlink" title="六、提取单独样式文件"></a>六、提取单独样式文件</h3><hr>
<p>有时候我们需要编译单独的 CSS 文件，这就需要 <a href="https://github.com/webpack/extract-text-webpack-plugin" target="_blank" rel="noopener">extract-text-webpack-plugin</a> 插件</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">npm install extract-text-webpack-plugin -D</span><br><span class="line">npm install style-loader css-loader -D</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack -w</span><br></pre></td></tr></table></figure>
<h3 id="七、自动打包公用资源"><a href="#七、自动打包公用资源" class="headerlink" title="七、自动打包公用资源"></a>七、自动打包公用资源</h3><hr>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">npm install jquery paging --save</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack -w</span><br></pre></td></tr></table></figure>
<h3 id="八、文件指纹-hash"><a href="#八、文件指纹-hash" class="headerlink" title="八、文件指纹 hash"></a>八、文件指纹 hash</h3><hr>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack --watch</span><br></pre></td></tr></table></figure>
<h3 id="九、异步加载"><a href="#九、异步加载" class="headerlink" title="九、异步加载"></a>九、异步加载</h3><hr>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack --watch</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2017/01/13/gulp/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2017/01/14/js-snippets-summary/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
